package com.epmis.epmis.Service.impl;

import com.epmis.epmis.Mapper.MissionMapper;
import com.epmis.epmis.Mapper.ProjectMapper;
import com.epmis.epmis.Mapper.UserMapper;
import com.epmis.epmis.Service.MissionService;
import com.epmis.epmis.pojo.*;
import com.epmis.epmis.utils.ImageUitl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.goeasy.GoEasy;
import org.checkerframework.checker.units.qual.A;
import org.springframework.data.relational.core.sql.In;
import org.springframework.remoting.jaxws.SimpleHttpServerJaxWsServiceExporter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MissionServiceImpl implements MissionService {
    @Resource
    MissionMapper missionMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    ProjectMapper projectMapper;

    @Override
    public HashMap<String,Object> getMissionMessage(int id) {
        Mission mission = missionMapper.getMission(id);
        HashMap<String,Object> missionMap = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int missionId = mission.getId();
        missionMap.put("number",missionId);
        missionMap.put("name",mission.getTitle());
        missionMap.put("projectId",mission.getProject_id());
        missionMap.put("projectName",projectMapper.getProjectMessage(mission.getProject_id()).getProject_name());
        if(mission.getPrincipal_id() == 0){
            missionMap.put("principal","无");
            missionMap.put("circleUrl","");
        }else{
            User user = userMapper.getUserMessage(mission.getPrincipal_id());
            missionMap.put("principalId",user.getId());
            missionMap.put("principal",user.getName());
            try {
                missionMap.put("circleUrl","data:image/jpeg;base64,"+ ImageUitl.ImageToBase64ByLocal(user.getPhoto()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(mission.getStart_date() == null){
            missionMap.put("start","");
        }else{
            missionMap.put("start",sdf.format(mission.getStart_date()));
        }
        if(mission.getOver_date() == null){
            missionMap.put("over","");
        }else{
            missionMap.put("over",sdf.format(mission.getOver_date()));
        }
        missionMap.put("creatorId",mission.getCreator_id());
        Project project = projectMapper.getProjectMessage(mission.getProject_id());
        missionMap.put("projectCreatorId",project.getCreator_id());
        missionMap.put("projectPrincipalId",project.getPrincipal_id());
        missionMap.put("createPerson",userMapper.getUserMessage(mission.getCreator_id()).getName());
        missionMap.put("createTime",sdf.format(mission.getCreate_date()));
        String memberArray = "";
        if (mission.getParticipant_id() != null){
            String[] members = mission.getParticipant_id().split(",");
            for(int i = 0; i < members.length ; i++) {
                if(i == members.length - 1){
                    memberArray = memberArray + members[i];
                }else if(i != 0){
                    memberArray = memberArray + members[i] + ",";
                }
            }
        }
        missionMap.put("participant",memberArray);
        if(mission.getPriority() == null){
            missionMap.put("priority","");
        }else{
            missionMap.put("priority",mission.getPriority());
        }
        if(mission.getDescription() == null){
            missionMap.put("description","");
        }else{
            missionMap.put("description",mission.getDescription());
        }

        missionMap.put("type",mission.getType());
        //分类
        switch (mission.getType()){
            case "项目目标":
                Target target = missionMapper.getTargetMessage(missionId);
                if(target.getUnit() == null){
                    missionMap.put("unit","");
                }else{
                    missionMap.put("unit",target.getUnit());
                }

                if(target.getInitial_value() == null){
                    missionMap.put("initial","");
                }else{
                    missionMap.put("initial",target.getInitial_value());
                }

                if(target.getTarget_value() == null){
                    missionMap.put("target","");
                }else{
                    missionMap.put("target",target.getTarget_value());
                }

                if(target.getCurrent_value() == null){
                    missionMap.put("current","");
                }else{
                    missionMap.put("current",target.getCurrent_value());
                }

                if(target.getStandard() == null){
                    missionMap.put("standard","");
                }else{
                    missionMap.put("standard",target.getStandard());
                }

                if(target.getTexplain() == null){
                    missionMap.put("explain","");
                }else{
                    missionMap.put("explain",target.getTexplain());
                }

                if(target.getTarget_status() == null){
                    missionMap.put("state","");
                }else{
                    missionMap.put("state",target.getTarget_status());
                }

                if(target.getFeedback() == null){
                        missionMap.put("feedback","");
                }else{
                    missionMap.put("feedback",target.getFeedback());
                }
                break;
            case "项目费用":
                Cost cost = missionMapper.getCostMessage(missionId);
                missionMap.put("state",cost.getCost_state());
                missionMap.put("finance",cost.getFinance());
                missionMap.put("budget",cost.getBudget());
                missionMap.put("execute",cost.getExecute());
                if(cost.getHappen_date() == null){
                    missionMap.put("occurrence","");
                }else{
                    missionMap.put("occurrence",sdf.format(cost.getHappen_date()));
                }
                if(cost.getPayment_date() == null){
                    missionMap.put("payment","");
                }else{
                    missionMap.put("payment",sdf.format(cost.getPayment_date()));
                }

                if(cost.getInformation() == null){
                    missionMap.put("payMessage","");
                }else{
                    missionMap.put("payMessage",cost.getInformation());
                }
                break;
            case "项目任务":
                MissionProject missionProject = missionMapper.getMissionProjectMessage(missionId);
                missionMap.put("state",missionProject.getPM_state());
                missionMap.put("stage",missionProject.getPhase());
                break;
        }
        return missionMap;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getMyPrincipalMissionA(int pageCode, int pageSize, String id) {
        PageHelper.startPage(pageCode,pageSize);
        List<Mission> missions = missionMapper.getMyPrincipalMissionA(Integer.valueOf(id));
        PageInfo pageInfo = new PageInfo<>(missions);
        List<Mission> mission = pageInfo.getList();
        List<HashMap<String,Object>> list = mission.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            if(res.getType().equals("项目目标")){
                newMap.put("status",missionMapper.getTargetMessage(res.getId()).getTarget_status());
                switch (missionMapper.getTargetMessage(res.getId()).getTarget_status()){
                    case "未开始":
                    case "失控":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "有风险":
                    case "验收中" :
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else if(res.getType().equals("项目费用")){
                newMap.put("status",missionMapper.getCostMessage(res.getId()).getCost_state());
                switch (missionMapper.getCostMessage(res.getId()).getCost_state()){
                    case "未发生":
                    case "财务退回":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "已发生":
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else{
                newMap.put("status",missionMapper.getMissionProjectMessage(res.getId()).getPM_state());
                switch (missionMapper.getMissionProjectMessage(res.getId()).getPM_state()){
                    case "未开始":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "进行中":
                        newMap.put("missionTag","warning");
                        break;
                    case "搁置中":
                        newMap.put("missionTag","primary");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }
            newMap.put("missionName",res.getTitle());
            newMap.put("id",res.getId());
            newMap.put("type",res.getType());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(res.getOver_date() != null){
                newMap.put("overDate",sdf.format(res.getOver_date()));
            }else{
                newMap.put("overDate","");
            }
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getMyPrincipalMissionC(int pageCode, int pageSize, String id) {
        PageHelper.startPage(pageCode,pageSize);
        List<Mission> missions = missionMapper.getMyPrincipalMissionC(Integer.valueOf(id));
        PageInfo pageInfo = new PageInfo<>(missions);
        List<Mission> mission = pageInfo.getList();
        List<HashMap<String,Object>> list = mission.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            if(res.getType().equals("项目目标")){
                newMap.put("status",missionMapper.getTargetMessage(res.getId()).getTarget_status());
                switch (missionMapper.getTargetMessage(res.getId()).getTarget_status()){
                    case "未开始":
                    case "失控":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "有风险":
                    case "验收中" :
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else if(res.getType().equals("项目费用")){
                newMap.put("status",missionMapper.getCostMessage(res.getId()).getCost_state());
                switch (missionMapper.getCostMessage(res.getId()).getCost_state()){
                    case "未发生":
                    case "财务退回":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "已发生":
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else{
                newMap.put("status",missionMapper.getMissionProjectMessage(res.getId()).getPM_state());
                switch (missionMapper.getMissionProjectMessage(res.getId()).getPM_state()){
                    case "未开始":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "进行中":
                        newMap.put("missionTag","warning");
                        break;
                    case "搁置中":
                        newMap.put("missionTag","primary");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }
            newMap.put("missionName",res.getTitle());
            newMap.put("id",res.getId());
            newMap.put("type",res.getType());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(res.getOver_date() != null){
                newMap.put("overDate",sdf.format(res.getOver_date()));
            }else{
                newMap.put("overDate","");
            }
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getMyPrincipalMissionS(int pageCode, int pageSize, String id, String content) {
        HashMap<String,Object> getMission = new HashMap<>();
        getMission.put("id",id);
        getMission.put("content",content);
        PageHelper.startPage(pageCode,pageSize);
        List<Mission> missions = missionMapper.getMyPrincipalMissionS(getMission);
        PageInfo pageInfo = new PageInfo<>(missions);
        List<Mission> mission = pageInfo.getList();
        List<HashMap<String,Object>> list = mission.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            if(res.getType().equals("项目目标")){
                newMap.put("status",missionMapper.getTargetMessage(res.getId()).getTarget_status());
                switch (missionMapper.getTargetMessage(res.getId()).getTarget_status()){
                    case "未开始":
                    case "失控":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "有风险":
                    case "验收中" :
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else if(res.getType().equals("项目费用")){
                newMap.put("status",missionMapper.getCostMessage(res.getId()).getCost_state());
                switch (missionMapper.getCostMessage(res.getId()).getCost_state()){
                    case "未发生":
                    case "财务退回":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "已发生":
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else{
                newMap.put("status",missionMapper.getMissionProjectMessage(res.getId()).getPM_state());
                switch (missionMapper.getMissionProjectMessage(res.getId()).getPM_state()){
                    case "未开始":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "进行中":
                        newMap.put("missionTag","warning");
                        break;
                    case "搁置中":
                        newMap.put("missionTag","primary");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }
            newMap.put("missionName",res.getTitle());
            newMap.put("id",res.getId());
            newMap.put("type",res.getType());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(res.getOver_date() != null){
                newMap.put("overDate",sdf.format(res.getOver_date()));
            }else{
                newMap.put("overDate","");
            }
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getHasMeMissionA(int pageCode, int pageSize, String id) {
        PageHelper.startPage(pageCode,pageSize);
        List<Mission> missions = missionMapper.getHasMeMissionA(Integer.valueOf(id));
        PageInfo pageInfo = new PageInfo<>(missions);
        List<Mission> mission = pageInfo.getList();
        List<HashMap<String,Object>> list = mission.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            if(res.getType().equals("项目目标")){
                newMap.put("status",missionMapper.getTargetMessage(res.getId()).getTarget_status());
                switch (missionMapper.getTargetMessage(res.getId()).getTarget_status()){
                    case "未开始":
                    case "失控":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "有风险":
                    case "验收中" :
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else if(res.getType().equals("项目费用")){
                newMap.put("status",missionMapper.getCostMessage(res.getId()).getCost_state());
                switch (missionMapper.getCostMessage(res.getId()).getCost_state()){
                    case "未发生":
                    case "财务退回":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "已发生":
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else{
                newMap.put("status",missionMapper.getMissionProjectMessage(res.getId()).getPM_state());
                switch (missionMapper.getMissionProjectMessage(res.getId()).getPM_state()){
                    case "未开始":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "进行中":
                        newMap.put("missionTag","warning");
                        break;
                    case "搁置中":
                        newMap.put("missionTag","primary");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }
            newMap.put("missionName",res.getTitle());
            newMap.put("id",res.getId());
            newMap.put("type",res.getType());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(res.getOver_date() != null){
                newMap.put("overDate",sdf.format(res.getOver_date()));
            }else{
                newMap.put("overDate","");
            }
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getHasMeMissionC(int pageCode, int pageSize, String id) {
        PageHelper.startPage(pageCode,pageSize);
        List<Mission> missions = missionMapper.getHasMeMissionC(Integer.valueOf(id));
        PageInfo pageInfo = new PageInfo<>(missions);
        List<Mission> mission = pageInfo.getList();
        List<HashMap<String,Object>> list = mission.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            if(res.getType().equals("项目目标")){
                newMap.put("status",missionMapper.getTargetMessage(res.getId()).getTarget_status());
                switch (missionMapper.getTargetMessage(res.getId()).getTarget_status()){
                    case "未开始":
                    case "失控":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "有风险":
                    case "验收中" :
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else if(res.getType().equals("项目费用")){
                newMap.put("status",missionMapper.getCostMessage(res.getId()).getCost_state());
                switch (missionMapper.getCostMessage(res.getId()).getCost_state()){
                    case "未发生":
                    case "财务退回":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "已发生":
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else{
                newMap.put("status",missionMapper.getMissionProjectMessage(res.getId()).getPM_state());
                switch (missionMapper.getMissionProjectMessage(res.getId()).getPM_state()){
                    case "未开始":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "进行中":
                        newMap.put("missionTag","warning");
                        break;
                    case "搁置中":
                        newMap.put("missionTag","primary");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }
            newMap.put("missionName",res.getTitle());
            newMap.put("id",res.getId());
            newMap.put("type",res.getType());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(res.getOver_date() != null){
                newMap.put("overDate",sdf.format(res.getOver_date()));
            }else{
                newMap.put("overDate","");
            }
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getHasMeMissionS(int pageCode, int pageSize, String id, String content) {
        HashMap<String,Object> getMission = new HashMap<>();
        getMission.put("id",id);
        getMission.put("content",content);
        PageHelper.startPage(pageCode,pageSize);
        List<Mission> missions = missionMapper.getHasMeMissionS(getMission);
        PageInfo pageInfo = new PageInfo<>(missions);
        List<Mission> mission = pageInfo.getList();
        List<HashMap<String,Object>> list = mission.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            if(res.getType().equals("项目目标")){
                newMap.put("status",missionMapper.getTargetMessage(res.getId()).getTarget_status());
                switch (missionMapper.getTargetMessage(res.getId()).getTarget_status()){
                    case "未开始":
                    case "失控":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "有风险":
                    case "验收中" :
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else if(res.getType().equals("项目费用")){
                newMap.put("status",missionMapper.getCostMessage(res.getId()).getCost_state());
                switch (missionMapper.getCostMessage(res.getId()).getCost_state()){
                    case "未发生":
                    case "财务退回":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "已发生":
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else{
                newMap.put("status",missionMapper.getMissionProjectMessage(res.getId()).getPM_state());
                switch (missionMapper.getMissionProjectMessage(res.getId()).getPM_state()){
                    case "未开始":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "进行中":
                        newMap.put("missionTag","warning");
                        break;
                    case "搁置中":
                        newMap.put("missionTag","primary");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }
            newMap.put("missionName",res.getTitle());
            newMap.put("id",res.getId());
            newMap.put("type",res.getType());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(res.getOver_date() != null){
                newMap.put("overDate",sdf.format(res.getOver_date()));
            }else{
                newMap.put("overDate","");
            }
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getMyCreateMissionA(int pageCode, int pageSize, String id) {
        PageHelper.startPage(pageCode,pageSize);
        List<Mission> missions = missionMapper.getMyCreateMissionA(Integer.valueOf(id));
        PageInfo pageInfo = new PageInfo<>(missions);
        List<Mission> mission = pageInfo.getList();
        List<HashMap<String,Object>> list = mission.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            if(res.getType().equals("项目目标")){
                newMap.put("status",missionMapper.getTargetMessage(res.getId()).getTarget_status());
                switch (missionMapper.getTargetMessage(res.getId()).getTarget_status()){
                    case "未开始":
                    case "失控":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "有风险":
                    case "验收中" :
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else if(res.getType().equals("项目费用")){
                newMap.put("status",missionMapper.getCostMessage(res.getId()).getCost_state());
                switch (missionMapper.getCostMessage(res.getId()).getCost_state()){
                    case "未发生":
                    case "财务退回":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "已发生":
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else{
                newMap.put("status",missionMapper.getMissionProjectMessage(res.getId()).getPM_state());
                switch (missionMapper.getMissionProjectMessage(res.getId()).getPM_state()){
                    case "未开始":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "进行中":
                        newMap.put("missionTag","warning");
                        break;
                    case "搁置中":
                        newMap.put("missionTag","primary");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }
            newMap.put("missionName",res.getTitle());
            newMap.put("id",res.getId());
            newMap.put("type",res.getType());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(res.getOver_date() != null){
                newMap.put("overDate",sdf.format(res.getOver_date()));
            }else{
                newMap.put("overDate","");
            }
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getMyCreateMissionC(int pageCode, int pageSize, String id) {
        PageHelper.startPage(pageCode,pageSize);
        List<Mission> missions = missionMapper.getMyCreateMissionC(Integer.valueOf(id));
        PageInfo pageInfo = new PageInfo<>(missions);
        List<Mission> mission = pageInfo.getList();
        List<HashMap<String,Object>> list = mission.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            if(res.getType().equals("项目目标")){
                newMap.put("status",missionMapper.getTargetMessage(res.getId()).getTarget_status());
                switch (missionMapper.getTargetMessage(res.getId()).getTarget_status()){
                    case "未开始":
                    case "失控":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "有风险":
                    case "验收中" :
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else if(res.getType().equals("项目费用")){
                newMap.put("status",missionMapper.getCostMessage(res.getId()).getCost_state());
                switch (missionMapper.getCostMessage(res.getId()).getCost_state()){
                    case "未发生":
                    case "财务退回":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "已发生":
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else{
                newMap.put("status",missionMapper.getMissionProjectMessage(res.getId()).getPM_state());
                switch (missionMapper.getMissionProjectMessage(res.getId()).getPM_state()){
                    case "未开始":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "进行中":
                        newMap.put("missionTag","warning");
                        break;
                    case "搁置中":
                        newMap.put("missionTag","primary");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }
            newMap.put("missionName",res.getTitle());
            newMap.put("id",res.getId());
            newMap.put("type",res.getType());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(res.getOver_date() != null){
                newMap.put("overDate",sdf.format(res.getOver_date()));
            }else{
                newMap.put("overDate","");
            }
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getMyCreateMissionS(int pageCode, int pageSize, String id, String content) {
        HashMap<String,Object> getMission = new HashMap<>();
        getMission.put("id",id);
        getMission.put("content",content);
        PageHelper.startPage(pageCode,pageSize);
        List<Mission> missions = missionMapper.getMyCreateMissionS(getMission);
        PageInfo pageInfo = new PageInfo<>(missions);
        List<Mission> mission = pageInfo.getList();
        List<HashMap<String,Object>> list = mission.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            if(res.getType().equals("项目目标")){
                newMap.put("status",missionMapper.getTargetMessage(res.getId()).getTarget_status());
                switch (missionMapper.getTargetMessage(res.getId()).getTarget_status()){
                    case "未开始":
                    case "失控":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "有风险":
                    case "验收中" :
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else if(res.getType().equals("项目费用")){
                newMap.put("status",missionMapper.getCostMessage(res.getId()).getCost_state());
                switch (missionMapper.getCostMessage(res.getId()).getCost_state()){
                    case "未发生":
                    case "财务退回":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "已发生":
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else{
                newMap.put("status",missionMapper.getMissionProjectMessage(res.getId()).getPM_state());
                switch (missionMapper.getMissionProjectMessage(res.getId()).getPM_state()){
                    case "未开始":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "进行中":
                        newMap.put("missionTag","warning");
                        break;
                    case "搁置中":
                        newMap.put("missionTag","primary");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }
            newMap.put("missionName",res.getTitle());
            newMap.put("id",res.getId());
            newMap.put("type",res.getType());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(res.getOver_date() != null){
                newMap.put("overDate",sdf.format(res.getOver_date()));
            }else{
                newMap.put("overDate","");
            }
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getMyDeleteMissionA(int pageCode, int pageSize, String id) {
        PageHelper.startPage(pageCode,pageSize);
        List<Mission> missions = missionMapper.getMyDeleteMissionA(Integer.valueOf(id));
        PageInfo pageInfo = new PageInfo<>(missions);
        List<Mission> mission = pageInfo.getList();
        List<HashMap<String,Object>> list = mission.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            if(res.getType().equals("项目目标")){
                newMap.put("status",missionMapper.getTargetMessage(res.getId()).getTarget_status());
                switch (missionMapper.getTargetMessage(res.getId()).getTarget_status()){
                    case "未开始":
                    case "失控":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "有风险":
                    case "验收中" :
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else if(res.getType().equals("项目费用")){
                newMap.put("status",missionMapper.getCostMessage(res.getId()).getCost_state());
                switch (missionMapper.getCostMessage(res.getId()).getCost_state()){
                    case "未发生":
                    case "财务退回":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "已发生":
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else{
                newMap.put("status",missionMapper.getMissionProjectMessage(res.getId()).getPM_state());
                switch (missionMapper.getMissionProjectMessage(res.getId()).getPM_state()){
                    case "未开始":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "进行中":
                        newMap.put("missionTag","warning");
                        break;
                    case "搁置中":
                        newMap.put("missionTag","primary");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }
            newMap.put("missionName",res.getTitle());
            newMap.put("id",res.getId());
            newMap.put("type",res.getType());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(res.getOver_date() != null){
                newMap.put("overDate",sdf.format(res.getOver_date()));
            }else{
                newMap.put("overDate","");
            }
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getMyDeleteMissionC(int pageCode, int pageSize, String id) {
        PageHelper.startPage(pageCode,pageSize);
        List<Mission> missions = missionMapper.getMyDeleteMissionC(Integer.valueOf(id));
        PageInfo pageInfo = new PageInfo<>(missions);
        List<Mission> mission = pageInfo.getList();
        List<HashMap<String,Object>> list = mission.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            if(res.getType().equals("项目目标")){
                newMap.put("status",missionMapper.getTargetMessage(res.getId()).getTarget_status());
                switch (missionMapper.getTargetMessage(res.getId()).getTarget_status()){
                    case "未开始":
                    case "失控":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "有风险":
                    case "验收中" :
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else if(res.getType().equals("项目费用")){
                newMap.put("status",missionMapper.getCostMessage(res.getId()).getCost_state());
                switch (missionMapper.getCostMessage(res.getId()).getCost_state()){
                    case "未发生":
                    case "财务退回":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "已发生":
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else{
                newMap.put("status",missionMapper.getMissionProjectMessage(res.getId()).getPM_state());
                switch (missionMapper.getMissionProjectMessage(res.getId()).getPM_state()){
                    case "未开始":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "进行中":
                        newMap.put("missionTag","warning");
                        break;
                    case "搁置中":
                        newMap.put("missionTag","primary");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }
            newMap.put("missionName",res.getTitle());
            newMap.put("id",res.getId());
            newMap.put("type",res.getType());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(res.getOver_date() != null){
                newMap.put("overDate",sdf.format(res.getOver_date()));
            }else{
                newMap.put("overDate","");
            }
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public PageInfo<HashMap<String, Object>> getMyDeleteMissionS(int pageCode, int pageSize, String id, String content) {
        HashMap<String,Object> getMission = new HashMap<>();
        getMission.put("id",id);
        getMission.put("content",content);
        PageHelper.startPage(pageCode,pageSize);
        List<Mission> missions = missionMapper.getMyDeleteMissionS(getMission);
        PageInfo pageInfo = new PageInfo<>(missions);
        List<Mission> mission = pageInfo.getList();
        List<HashMap<String,Object>> list = mission.stream().map(res -> {
            HashMap<String,Object> newMap = new HashMap<>();
            if(res.getType().equals("项目目标")){
                newMap.put("status",missionMapper.getTargetMessage(res.getId()).getTarget_status());
                switch (missionMapper.getTargetMessage(res.getId()).getTarget_status()){
                    case "未开始":
                    case "失控":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "有风险":
                    case "验收中" :
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else if(res.getType().equals("项目费用")){
                newMap.put("status",missionMapper.getCostMessage(res.getId()).getCost_state());
                switch (missionMapper.getCostMessage(res.getId()).getCost_state()){
                    case "未发生":
                    case "财务退回":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "已发生":
                        newMap.put("missionTag","warning");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }else{
                newMap.put("status",missionMapper.getMissionProjectMessage(res.getId()).getPM_state());
                switch (missionMapper.getMissionProjectMessage(res.getId()).getPM_state()){
                    case "未开始":
                    case "关闭":
                        newMap.put("missionTag","danger");
                        break;
                    case "进行中":
                        newMap.put("missionTag","warning");
                        break;
                    case "搁置中":
                        newMap.put("missionTag","primary");
                        break;
                    default:
                        newMap.put("missionTag","success");
                        break;
                }
            }
            newMap.put("missionName",res.getTitle());
            newMap.put("id",res.getId());
            newMap.put("type",res.getType());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(res.getOver_date() != null){
                newMap.put("overDate",sdf.format(res.getOver_date()));
            }else{
                newMap.put("overDate","");
            }
            return newMap;
        }).collect(Collectors.toList());
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public List<HashMap<String, Object>> getSelectSonMission(int id) {
        List<MissionProject> missionProjects = missionMapper.getSelectSonMission(id);
        List<HashMap<String,Object>> list = missionProjects.stream().map(res -> {
            HashMap<String,Object> map = new HashMap<>();
            Mission mission = missionMapper.getMission(res.getMission_id());
            map.put("missionName",mission.getTitle());
            map.put("status",res.getPM_state());
            map.put("id",mission.getId());
            return map;
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public List<HashMap<String, Object>> getSelectRelMission(int id, int enterpriseId) {
        List<MissionProject> missionProjects = missionMapper.getSelectRelMission(id,enterpriseId);
        List<HashMap<String,Object>> list = missionProjects.stream().map(res -> {
            HashMap<String,Object> map = new HashMap<>();
            Mission mission = missionMapper.getMission(res.getMission_id());
            map.put("missionName",mission.getTitle());
            map.put("status",res.getPM_state());
            map.put("id",mission.getId());
            return map;
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public List<HashMap<String, Object>> getSelectBeforeMission(int id, int enterpriseId) {
        List<Mission> missions = missionMapper.getSelectBeforeMission(id,enterpriseId);
        List<HashMap<String,Object>> list = missions.stream().map(res -> {
            HashMap<String,Object> map = new HashMap<>();
            map.put("missionName",res.getTitle());
            if(res.getType().equals("项目目标")){
                map.put("status",missionMapper.getTargetMessage(res.getId()).getTarget_status());
            }else if(res.getType().equals("项目费用")){
                map.put("status",missionMapper.getCostMessage(res.getId()).getCost_state());
            }else{
                map.put("status",missionMapper.getMissionProjectMessage(res.getId()).getPM_state());
            }
            map.put("id",res.getId());
            return map;
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public List<HashMap<String, Object>> getSelectAfterMission(int id, int enterpriseId) {
        List<Mission> missions = missionMapper.getSelectAfterMission(id,enterpriseId);
        List<HashMap<String,Object>> list = missions.stream().map(res -> {
            HashMap<String,Object> map = new HashMap<>();
            map.put("missionName",res.getTitle());
            if(res.getType().equals("项目目标")){
                map.put("status",missionMapper.getTargetMessage(res.getId()).getTarget_status());
            }else if(res.getType().equals("项目费用")){
                map.put("status",missionMapper.getCostMessage(res.getId()).getCost_state());
            }else{
                map.put("status",missionMapper.getMissionProjectMessage(res.getId()).getPM_state());
            }
            map.put("id",res.getId());
            return map;
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public  List<HashMap<String,Object>> getMissionByParticipant(int id) {
        List<Mission> missions = missionMapper.getMissionByParticipant(id);
        List<HashMap<String,Object>> list = missions.stream().map(res -> {
            HashMap<String,Object> map = new HashMap<>();
            map.put("label","编号："+ res.getId() + "，标题：" + res.getTitle());
            map.put("value",res.getId());
            return map;
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public HashMap<String, Object> getMissionWorkTime(int id) {
        HashMap<String,Object> map = new HashMap<>();
        MissionProject missionProject = missionMapper.getMissionProjectMessage(id);
        map.put("prospect",missionProject.getProspect_hour());
        map.put("hour",missionProject.getHour());
        return map;
    }

    @Override
    public int updateSonMission(ArrayList list,int id) {
        for(int i = 0 ; i < list.size(); i++){
            missionMapper.updateSonMission(id, (Integer) list.get(i));
        }
        return 1;
    }

    @Override
    public int updateProspect(int id, double prospect) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("prospect",prospect);
        return missionMapper.updateProspect(map);
    }

    @Override
    public int updateRelMission(ArrayList list, int id) {
        HashMap<String,Object> map = new HashMap<>();
        for(int i = 0 ; i < list.size(); i++){
            String content = "";
            map.put("missionId",list.get(i));
            String relMission = missionMapper.getMissionProjectMessage((Integer) list.get(i)).getRel_mission_id();
            if(relMission != null && !relMission.equals("")){
                content = missionMapper.getMissionProjectMessage((Integer) list.get(i)).getRel_mission_id();
            }
            if(content.equals("")){
                content = "," + id + ",";
            }else{
                content += id + ",";
            }
            map.put("content",content);
            missionMapper.updateRelMission(map);
        }
        return 1;
    }

    @Override
    public int updateBeforeMission(ArrayList list, int id) {
        HashMap<String,Object> map = new HashMap<>();
        for(int i = 0 ; i < list.size(); i++){
            String content = "";
            map.put("id",list.get(i));
            String beforeMission = missionMapper.getMission((Integer) list.get(i)).getAfter_id();
            if(beforeMission != null && !beforeMission.equals("")){
                content = missionMapper.getMission((Integer) list.get(i)).getAfter_id();
            }
            if(content.equals("")){
                content = "," + id + ",";
            }else{
                content += id + ",";
            }
            map.put("content",content);
            missionMapper.updateAfterMission(map);
        }

        String content = "";
        map.put("id",id);
        for(int i = 0;i < list.size();i++){
            String afterMission = missionMapper.getMission(id).getBefore_id();
            if(afterMission != null && !afterMission.equals("")){
                content = missionMapper.getMission(id).getBefore_id();
            }
            if(content.equals("")){
                content = "," + list.get(i) + ",";
            }else{
                content += list.get(i) + ",";
            }
        }
        map.put("content",content);
        missionMapper.updateBeforeMission(map);
        return 1;
    }

    @Override
    public int updateAfterMission(ArrayList list, int id) {
        HashMap<String,Object> map = new HashMap<>();
        for(int i = 0 ; i < list.size(); i++){
            String content = "";
            map.put("id",list.get(i));
            String afterMission = missionMapper.getMission((Integer) list.get(i)).getBefore_id();
            if(afterMission != null && !afterMission.equals("")){
                content = missionMapper.getMission((Integer) list.get(i)).getBefore_id();
            }
            if(content.equals("")){
                content = "," + id + ",";
            }else{
                content += id + ",";
            }
            map.put("content",content);
            missionMapper.updateBeforeMission(map);
        }

        String content = "";
        map.put("id",id);
        for(int i = 0;i < list.size();i++){
            String beforeMission = missionMapper.getMission(id).getAfter_id();
            if(beforeMission != null && !beforeMission.equals("")){
                content = missionMapper.getMission(id).getAfter_id();
            }
            if(content.equals("")){
                content = "," + list.get(i) + ",";
            }else{
                content += list.get(i) + ",";
            }
        }
        map.put("content",content);
        missionMapper.updateAfterMission(map);
        return 1;
    }

    @Override
    public int updateMissionTitle(String title, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("title",title);
        map.put("id",id);
        Date date = new Timestamp((new java.util.Date()).getTime());
        Activity activity = new Activity();
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改标题");
        activity.setUpdate_before(missionMapper.getMission(id).getTitle());
        activity.setUpdate_after(title);
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateMissionTitle(map);
    }

    @Override
    public int updateMissionStatus(int status, int id) {
        if(status == 2){
            List<Mission> missions = missionMapper.getBeforeMission(id);
            for(int i = 0;i < missions.size();i++){
                if(missions.get(i).getStatus() != 2){
                    return 0;
                }
            }
        }
        return missionMapper.updateMissionStatus(status,id);
    }

    @Override
    public int updateMissionProjectStatus(String status, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("status",status);
        map.put("id",id);
        Date date = new Timestamp((new java.util.Date()).getTime());
        Activity activity = new Activity();
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改状态");
        activity.setUpdate_before(missionMapper.getMissionProjectMessage(id).getPM_state());
        activity.setUpdate_after(status);
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateMissionProjectStatus(map);
    }

    @Override
    public int updateTargetStatus(String status, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("status",status);
        map.put("id",id);
        Activity activity = new Activity();
        Date date = new Timestamp((new java.util.Date()).getTime());
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改状态");
        activity.setUpdate_before(missionMapper.getTargetMessage(id).getTarget_status());
        activity.setUpdate_after(status);
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateTargetStatus(map);
    }

    @Override
    public int updateCostStatus(String status, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("status",status);
        map.put("id",id);
        Activity activity = new Activity();
        Date date = new Timestamp((new java.util.Date()).getTime());
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改状态");
        activity.setUpdate_before(missionMapper.getCostMessage(id).getCost_state());
        activity.setUpdate_after(status);
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateCostStatus(map);
    }

    @Override
    public int updateMissionPrincipal(int principal_id, int id,int userId,int enterpriseId) {
        Activity activity = new Activity();
        Date date = new Timestamp((new java.util.Date()).getTime());
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改负责人");
        User principal = userMapper.getUserMessage(missionMapper.getMission(id).getPrincipal_id());
        User newPrincipal = userMapper.getUserMessage(principal_id);
        if(principal == null){
            activity.setUpdate_before("");
        }else{
            activity.setUpdate_before(principal.getName()+ ":" + principal.getUsername());
        }
        activity.setUpdate_after(newPrincipal.getName() + ":" + newPrincipal.getUsername());
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateMissionPrincipal(principal_id,id);
    }

    @Override
    public int updateMissionStart(Date start_date, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("start_date",start_date);
        map.put("id",id);
        Activity activity = new Activity();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Timestamp((new java.util.Date()).getTime());
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改开始日期");
        if(missionMapper.getMission(id).getStart_date() == null){
            activity.setUpdate_before("");
        }else{
            activity.setUpdate_before(sdf.format(missionMapper.getMission(id).getStart_date()));
        }
        activity.setUpdate_after(sdf.format(start_date));
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateMissionStart(map);
    }

    @Override
    public int updateMissionOver(Date over_date, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("over_date",over_date);
        map.put("id",id);
        Activity activity = new Activity();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Timestamp((new java.util.Date()).getTime());
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改截止日期");
        if(missionMapper.getMission(id).getOver_date() == null){
            activity.setUpdate_before("");
        }else{
            activity.setUpdate_before(sdf.format(missionMapper.getMission(id).getOver_date()));
        }
        activity.setUpdate_after(sdf.format(over_date));
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateMissionOver(map);
    }

    @Override
    public int updateMissionPhase(String phase, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("phase",phase);
        map.put("id",id);
        Date date = new Timestamp((new java.util.Date()).getTime());
        Activity activity = new Activity();
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改项目阶段");
        activity.setUpdate_before(missionMapper.getMissionProjectMessage(id).getPhase());
        activity.setUpdate_after(phase);
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateMissionPhase(map);
    }

    @Override
    public int updateMissionPriority(String priority, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("priority",priority);
        map.put("id",id);
        Date date = new Timestamp((new java.util.Date()).getTime());
        Activity activity = new Activity();
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改优先级");
        activity.setUpdate_before(missionMapper.getMission(id).getPriority());
        activity.setUpdate_after(priority);
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateMissionPriority(map);
    }

    @Override
    public int updateMissionDescription(String description, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("description",description);
        map.put("id",id);
        Date date = new Timestamp((new java.util.Date()).getTime());
        Activity activity = new Activity();
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改描述");
        activity.setUpdate_before(missionMapper.getMission(id).getDescription());
        activity.setUpdate_after(description);
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateMissionDescription(map);
    }

    @Override
    public int updateMissionParticipant(String participant, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("participant_id",participant);
        map.put("id",id);
        Date date = new Timestamp((new java.util.Date()).getTime());
        Activity activity = new Activity();
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改参与人");
        activity.setUpdate_before(missionMapper.getMission(id).getParticipant_id());
        activity.setUpdate_after(participant);
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateMissionParticipant(map);
    }

    @Override
    public int updateTargetUnit(String unit, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("unit",unit);
        map.put("id",id);
        Date date = new Timestamp((new java.util.Date()).getTime());
        Activity activity = new Activity();
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改目标单位");
        activity.setUpdate_before(missionMapper.getTargetMessage(id).getUnit());
        activity.setUpdate_after(unit);
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateTargetUnit(map);
    }

    @Override
    public int updateTargetInitial(String initial_value, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("initial_value",initial_value);
        map.put("id",id);
        Date date = new Timestamp((new java.util.Date()).getTime());
        Activity activity = new Activity();
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改起始值");
        activity.setUpdate_before(missionMapper.getTargetMessage(id).getInitial_value());
        activity.setUpdate_after(initial_value);
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateTargetInitial(map);
    }

    @Override
    public int updateTargetTarget(String target_value, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("target_value",target_value);
        map.put("id",id);
        Date date = new Timestamp((new java.util.Date()).getTime());
        Activity activity = new Activity();
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改目标值");
        activity.setUpdate_before(missionMapper.getTargetMessage(id).getTarget_value());
        activity.setUpdate_after(target_value);
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateTargetTarget(map);
    }

    @Override
    public int updateTargetCurrent(String current_value, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("current_value",current_value);
        map.put("id",id);
        Date date = new Timestamp((new java.util.Date()).getTime());
        Activity activity = new Activity();
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改当前值");
        activity.setUpdate_before(missionMapper.getTargetMessage(id).getCurrent_value());
        activity.setUpdate_after(current_value);
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateTargetCurrent(map);
    }

    @Override
    public int updateTargetExplain(String explain, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("explain",explain);
        map.put("id",id);
        Date date = new Timestamp((new java.util.Date()).getTime());
        Activity activity = new Activity();
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改交付说明");
        activity.setUpdate_before(missionMapper.getTargetMessage(id).getTexplain());
        activity.setUpdate_after(explain);
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateTargetExplain(map);
    }

    @Override
    public int updateTargetFeedback(String feedback, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("feedback",feedback);
        map.put("id",id);
        Date date = new Timestamp((new java.util.Date()).getTime());
        Activity activity = new Activity();
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改验收反馈");
        activity.setUpdate_before(missionMapper.getTargetMessage(id).getFeedback());
        activity.setUpdate_after(feedback);
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateTargetFeedback(map);
    }

    @Override
    public int updateTargetStandard(String standard, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("standard",standard);
        map.put("id",id);
        Date date = new Timestamp((new java.util.Date()).getTime());
        Activity activity = new Activity();
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改验收标准");
        activity.setUpdate_before(missionMapper.getTargetMessage(id).getStandard());
        activity.setUpdate_after(standard);
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateTargetStandard(map);
    }

    @Override
    public int updateCostFinance(String finance, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("finance",finance);
        map.put("id",id);
        Date date = new Timestamp((new java.util.Date()).getTime());
        Activity activity = new Activity();
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改财务科目");
        activity.setUpdate_before(missionMapper.getCostMessage(id).getFinance());
        activity.setUpdate_after(finance);
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateCostFinance(map);
    }

    @Override
    public int updateCostBudget(double budget, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("budget",budget);
        map.put("id",id);
        Date date = new Timestamp((new java.util.Date()).getTime());
        Activity activity = new Activity();
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改预算金额");
        activity.setUpdate_before(String.valueOf(missionMapper.getCostMessage(id).getBudget()));
        activity.setUpdate_after(String.valueOf(budget));
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateCostBudget(map);
    }

    @Override
    public int updateCostExecute(double execute, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("execute",execute);
        map.put("id",id);
        Date date = new Timestamp((new java.util.Date()).getTime());
        Activity activity = new Activity();
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改执行金额");
        activity.setUpdate_before(String.valueOf(missionMapper.getCostMessage(id).getExecute()));
        activity.setUpdate_after(String.valueOf(execute));
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateCostExecute(map);
    }

    @Override
    public int updateCostHappen(Date happen_date, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("happen_date",happen_date);
        map.put("id",id);
        Date date = new Timestamp((new java.util.Date()).getTime());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Activity activity = new Activity();
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改发生日期");
        if(missionMapper.getCostMessage(id).getHappen_date() == null){
            activity.setUpdate_before("");
        }else{
            activity.setUpdate_before(sdf.format(missionMapper.getCostMessage(id).getHappen_date()));
        }
        activity.setUpdate_after(sdf.format(happen_date));
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateCostHappen(map);
    }

    @Override
    public int updateCostPay(Date payment_id, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("payment_date",payment_id);
        map.put("id",id);
        Date date = new Timestamp((new java.util.Date()).getTime());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Activity activity = new Activity();
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改付款日期");
        if(missionMapper.getCostMessage(id).getPayment_date() == null){
            activity.setUpdate_before("");
        }else{
            activity.setUpdate_before(sdf.format(missionMapper.getCostMessage(id).getPayment_date()));
        }
        activity.setUpdate_after(sdf.format(payment_id));
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateCostPay(map);
    }

    @Override
    public int updateMissionDelete(int id) {
        return missionMapper.updateMissionDelete(id);
    }

    @Override
    public int updateCostInformation(String information, int id,int userId,int enterpriseId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("information",information);
        map.put("id",id);
        Date date = new Timestamp((new java.util.Date()).getTime());
        Activity activity = new Activity();
        activity.setMission_id(id);
        activity.setUpdate_p_id(userId);
        activity.setUpdate_content("修改付款信息");
        activity.setUpdate_before(missionMapper.getCostMessage(id).getInformation());
        activity.setUpdate_after(information);
        activity.setEnterprise_id(enterpriseId);
        activity.setUpdate_date(date);
        userMapper.createActivity(activity);
        return missionMapper.updateCostInformation(map);
    }

    @Override
    public int deleteSonMission(int id) {
        return missionMapper.deleteSonMission(id);
    }

    @Override
    public int deleteRelMission(int id,int missionId) {
        String content = "";
        String[] RelMission = missionMapper.getMissionProjectMessage(missionId).getRel_mission_id().split(",");
        for(int i = 0;i < RelMission.length;i++){
            if(RelMission.length != 2){
                if(i == 0){
                    content = ",";
                }else if(!RelMission[i].equals(String.valueOf(id)) && i != 0){
                    content += RelMission[i] + ",";
                }
            }
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("missionId",missionId);
        map.put("content",content);
        missionMapper.updateRelMission(map);
        return 1;
    }

    @Override
    public int createWorkTime(HashMap<String, Object> map) throws ParseException {
        Hour hour = new Hour();
        hour.setMission_id((Integer) map.get("missionValue"));
        hour.setDescription((String) map.get("workContent"));
        hour.setEnterprise_id(Integer.valueOf((String)map.get("enterprise_id")));
        hour.setRegistrant_id(Integer.valueOf((String)map.get("creatorId")));
        hour.setHour(Double.valueOf(map.get("workTime").toString()));
        hour.setWork_type((String) map.get("workType"));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        hour.setWork_date(sdf.parse((String) map.get("workDate")));
        Double workHour = missionMapper.getMissionProjectMessage((Integer) map.get("missionValue")).getHour();
        workHour += Double.valueOf(map.get("workTime").toString());
        HashMap<String,Object> newMap = new HashMap<>();
        newMap.put("id",map.get("missionValue"));
        newMap.put("hour",workHour);
        missionMapper.updateWorkTime(newMap);
        return missionMapper.createWorkTime(hour);
    }

    @Override
    public int deleteBeforeMission(int id, int missionId) {
        String content = "";
        String[] afterMission = missionMapper.getMission(missionId).getAfter_id().split(",");
        for(int i = 0;i < afterMission.length;i++){
            if(afterMission.length != 2){
                if(i == 0){
                    content = ",";
                }else if(!afterMission[i].equals(String.valueOf(id)) && i != 0){
                    content += afterMission[i] + ",";
                }
            }
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("id",missionId);
        map.put("content",content);
        missionMapper.updateAfterMission(map);

        content = "";
        String[] beforeMission = missionMapper.getMission(id).getBefore_id().split(",");
        for(int i = 0;i < beforeMission.length;i++){
            if(beforeMission.length != 2){
                if(i == 0){
                    content = ",";
                }else if(!beforeMission[i].equals(String.valueOf(missionId)) && i != 0){
                    content += beforeMission[i] + ",";
                }
            }
        }
        map.put("id",id);
        map.put("content",content);
        missionMapper.updateBeforeMission(map);
        return 1;
    }


    @Override
    public int createMissionComment(HashMap<String, Object> map) {
        GoEasy goEasy = new GoEasy("rest-hangzhou.goeasy.io","BC-f92a140e428c4ce79442bafc26e543d8");
        Comment comment = new Comment();
        Date date = new Timestamp((new java.util.Date()).getTime());
        if((int)map.get("replyCommentId") != 0){
            int replyUserId = missionMapper.getComment((int)map.get("replyCommentId")).getIssuer_id();
            User Isuser = userMapper.getUserMessage(Integer.valueOf((String) map.get("userId")));
            Notification notification = new Notification();
            notification.setContent((String)map.get("content"));
            notification.setActivity("回复了你的评论");
            notification.setType("评论");
            notification.setDate(date);
            notification.setIssuer_name(Isuser.getName());
            notification.setIssuer_photo(Isuser.getPhoto());
            notification.setIssuer_id(Isuser.getId());
            notification.setRecipient_id(replyUserId);
            notification.setEnterprise_id(Integer.valueOf((String)map.get("enterpriseId")));
            userMapper.createNotification(notification);
            goEasy.publish(String.valueOf(replyUserId), "您有一条关于评论的新消息，请到消息查看详情");
        }
        comment.setComment_date(date);
        comment.setIssuer_id(Integer.valueOf((String) map.get("userId")));
        comment.setContent((String)map.get("content"));
        comment.setEnterprise_id(Integer.valueOf((String)map.get("enterpriseId")));
        comment.setMission_id((int)map.get("id"));
        return missionMapper.createMissionComment(comment);
    }

    @Override
    public int deleteMissionComment(int id) {
        return missionMapper.deleteMissionComment(id);
    }

    @Override
    public int deleteWorkTime(int id) {
        Hour hour = missionMapper.getHour(id);
        double workHour = hour.getHour();
        double missionHour = missionMapper.getMissionProjectMessage(hour.getMission_id()).getHour();
        double result = missionHour - workHour;
        HashMap<String,Object> map = new HashMap<>();
        map.put("id",hour.getMission_id());
        map.put("hour",result);
        missionMapper.updateWorkTime(map);
        return missionMapper.deleteWorkTime(id);
    }

    @Override
    public int createMissionFile(HashMap<String, Object> map) {
        Accessory accessory = new Accessory();
        Date date = new Timestamp((new java.util.Date()).getTime());
        accessory.setAccessory_name((String) map.get("fileName"));
        accessory.setAccessory_size((String) map.get("size"));
        accessory.setEnterprise_id(Integer.valueOf((String)map.get("enterpriseId")));
        accessory.setUploading_date(date);
        accessory.setUploading_p_id(Integer.valueOf((String)map.get("userId")));
        accessory.setUrl((String) map.get("url"));
        accessory.setMission_id((int)map.get("missionId"));
        return missionMapper.createFile(accessory);
    }

    @Override
    public int deleteMissionFile(int id) {
        return missionMapper.deleteMissionFile(id);
    }

    @Override
    public Accessory getMissionFileById(int fileId) {
        return missionMapper.getMissionFileById(fileId);
    }

    @Override
    public int deleteMission(int id) {
        return missionMapper.deleteMission(id);
    }

    @Override
    public int createExamineFile(HashMap<String, Object> map) {
        Accessory accessory = new Accessory();
        Date date = new Timestamp((new java.util.Date()).getTime());
        accessory.setAccessory_name((String) map.get("fileName"));
        accessory.setAccessory_size((String) map.get("size"));
        accessory.setEnterprise_id(Integer.valueOf((String)map.get("enterpriseId")));
        accessory.setUploading_date(date);
        accessory.setUploading_p_id(Integer.valueOf((String)map.get("userId")));
        accessory.setUrl((String) map.get("url"));
        accessory.setExamine_id((int)map.get("ExamineId"));
        return missionMapper.createFile(accessory);
    }


}
